<?php

namespace app\modules\agent\controllers;
use app\models\AgentProfit;
use app\models\UserCardRecharge as Recharge;
use app\models\AgentBill;
use app\models\Agent;
use Exception;
use Yii;
use yii\data\Pagination;
class ProfitController extends BaseController{

	public function actionList(){
		$query = AgentProfit::find()->alias('profit');
        $query->joinWith('agent agent');
        $query->joinWith('recharge recharge');
        $query->joinWith('package package');
        $query->where(['profit.agent_id' => $this->agent->id]);
        $query->andFilterWhere(['recharge.trade_no' => $this->get('search_trade_no')]);
        $pagination = new Pagination(['totalCount' => $query->count()]);
        $query->orderBy('profit.id DESC');
        $query->offset($pagination->offset)->limit($pagination->limit);
        $profitList = $query->all();
        return $this->render('profit_list', [
            'profitList' => $profitList,
            'pagination' => $pagination,
        ]);

	}

	public function actionRechargeView(){
		$id = $this->get('id');
		$recharge = Recharge::findOne(['id'=>$id]);
        $this->layout = false; 
        $this->layout = "agent_new";
		return $this->render('recharge_view', [
            'recharge' => $recharge,
        ]);

	}
    public function actionCommissionList(){
        $agent = Agent::findOne(['id'=>$this->agent->id]);
        $query = AgentBill::find();
        $query->andWhere(['agent_id'=>$agent->id]);
        $query->andWhere(['status'=>2]);
        $pagination = new Pagination(['totalCount' => $query->count()]);
        $query->orderBy('day DESC');
        $query->offset($pagination->offset)->limit($pagination->limit);
        $commissionList = $query->all();
        return $this->render('commission_list',[
            'agent' => $agent,
            'commissionList' => $commissionList,
            'pagination' => $pagination,
        ]);
    }
}